package com.testapp.bluetoothtest08.Util;

import com.alibaba.fastjson.JSONObject;
import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.File.ChallengeFile;
import com.testapp.bluetoothtest08.File.Cipher;
import com.testapp.bluetoothtest08.File.GenerateKeyFile;
import com.testapp.bluetoothtest08.File.ScanCodeFirstRequestFile;
import com.testapp.bluetoothtest08.File.ScanCodeSecondRequestFile;
import com.testapp.bluetoothtest08.File.ScanMachineFile;
import com.testapp.bluetoothtest08.File.UserScanCodeDataFile;
import com.testapp.bluetoothtest08.Http.OkHttp;
import com.testapp.bluetoothtest08.cipher.UserCipherService;
import com.testapp.bluetoothtest08.cipher.entity.UserUpdate;
import com.testapp.bluetoothtest08.cipher.entity.user.UserPrivateData;
import com.testapp.bluetoothtest08.cipher.entity.user.UserPrivateDataStr;
import com.testapp.bluetoothtest08.cipher.entity.user.UserScanCodeData;
import com.testapp.bluetoothtest08.cipher.impl.UserCipherServiceImpl;

import java.math.BigInteger;


public class RequestCodeGenerateUtil {

    private String key = null;
    private OkHttp okHttp = new OkHttp();
    private String cipher;
    private UserCipherService userCipherService = new UserCipherServiceImpl();
    private UserUpdate userUD = new UserUpdate();

    public void requestUpdate() {
        System.out.println("进入 定时任务 ");
        key = new GenerateKeyFile().read();
        if (key != null) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("key -- > " + key);
                    String re = okHttp.httpPost(Constants.REQUEST_GENERATE_CODE, key);
                    if (re != null) {
                        System.out.println("服务器返回 requestCode ---> " + re);
                        JSONObject o = JSONObject.parseObject(re);
                        String d = o.getString("data");
                        if (d != null) {
                            JSONObject jsonObject = JSONObject.parseObject(d);
                            userUD.AD = jsonObject.getBigInteger("ad");
                            userUD.y_ = jsonObject.getBigInteger("y");
                            System.out.println("userUD ----> "+userUD);
                            // ------------------------- 存入扫码机序列和时间 ----------------------------------
                            StringBuffer sb = new StringBuffer();
                            sb.append(jsonObject.getString("sn"));
                            sb.append(";");
                            //写入文件
                            int day = new ScanMachineFile().readIndex();
                            if (new ScanMachineFile().readTime(day) == 0) {
                                new ScanMachineFile().writeTime(day);
                            }
                            if (System.currentTimeMillis() - new ScanMachineFile().readTime(day) >= 60 * 60 * 24 * 1000) {
                                day = day + 1;
                                if (day == 14) {
                                    day = 0;
                                }
                                new ScanMachineFile().writeIndex(day);
                            }
                            if (System.currentTimeMillis() - new ScanMachineFile().readTime(day) >= 14 * 60 * 60 * 24 * 1000) {
                                new ScanMachineFile().clearData(day);
                                new ScanMachineFile().writeTime(day);
                            }
                            System.out.println("sanID and time --->"+sb.toString());
                            new ScanMachineFile().writeData(day, sb.toString());
                            // ------------------------- 存入扫码机序列和时间 ----------------------------------

                            //第一阶段的密码
                            cipher = new Cipher().read();
                            System.out.println("更新前数据 -----> " + cipher);
                            UserPrivateDataStr cipherUPDS = JSONObject.parseObject(cipher, UserPrivateDataStr.class);
                            //转换成 UserPrivateData
                            UserPrivateData upd = new UserPrivateData(cipherUPDS);
                            //获得challenge
                            String challenge = new ChallengeFile().read();
                            String userScanCodeData = new UserScanCodeDataFile().read();
                            UserScanCodeData uscd = JSONObject.parseObject(userScanCodeData, UserScanCodeData.class);

                            if (userCipherService.changePrivateData(upd, uscd, userUD)) {
                                UserPrivateDataStr UPDS = new UserPrivateDataStr(upd);
                                String secondCipher = JSONObject.toJSONString(UPDS);
                                System.out.println("更新后数据 ---- >" + secondCipher);
                                new Cipher().write(secondCipher);
                                System.out.println("更新后数据 ---- >" + new Cipher().read());
//                    String last = new CipherSecond().read();
//                    System.out.println(last);
                                //清除
//                    if (last != null) {
                                new ChallengeFile().clear();
                                new GenerateKeyFile().clear();
                                new ScanCodeFirstRequestFile().clear();
                                new ScanCodeSecondRequestFile().clear();
                                new UserScanCodeDataFile().clear();
//                    }


                            }
                        }
                    }
                }
            });
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
}
//    ScanCodeFirstRequest scfr = (ScanCodeFirstRequest) list.get(1);
//    ScanCodeSecondRequest scsr = (ScanCodeSecondRequest) list.get(2);


//    String first = JSONObject.toJSONString(scfr);
//    String second = JSONObject.toJSONString(scsr);
//                        new ChallengeFile().write(challenge);
//                                new ScanCodeFirstRequestFile().write(first);
//                                new ScanCodeSecondRequestFile().write(second);